package com.hs.excel.demos.listenter;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hs.excel.demos.pojo.RequestParm;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class updateListener extends AnalysisEventListener<Map<Integer, String>> {


    private RequestParm requestParm;
    private Map<Integer, String> head;


    public updateListener(RequestParm a) {
        this.requestParm = a;
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext analysisContext) {
        Boolean flag = Comont.ifParamNot(requestParm);
        // 有条件
        if (flag) {
            List<String> terms = requestParm.getTerms();
            StringBuilder stringBuilder = new StringBuilder("update " + requestParm.getTableName() + " set ");
            List<String> heads = new ArrayList<>(head.values());
            List<String> values = new ArrayList<>(data.values());
            StringBuilder whereStrng = new StringBuilder(" where ");

            for (int i = 0; i < heads.size(); i++) {
                if (terms.contains(heads.get(i))) {
                    whereStrng.append(heads.get(i) + "=" + "'" + values.get(i) + "'" + "and");
                } else {
                    String s = heads.get(i) + "=" + "'" + values.get(i) + "'" + ",";
                    stringBuilder.append(s);

                }
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1); // 去除最后的，
            if (whereStrng.length() > 6) {
                whereStrng.delete(whereStrng.length() - 3, whereStrng.length());// 去除最后的 and
            }
            // 拼装sql
            stringBuilder.append(whereStrng);
            stringBuilder.append(";");
            Comont.updateList.add(stringBuilder.toString());

            // 没条件
        } else {
            StringBuilder stringBuilder = new StringBuilder("update " + requestParm.getTableName() + " set ");
            List<String> heads = new ArrayList<>(head.values());
            List<String> values = new ArrayList<>(data.values());

            for (int i = 0; i < heads.size(); i++) {
                String s = heads.get(i) + "=" + "'" + values.get(i) + "'" + ",";
                stringBuilder.append(s);
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            stringBuilder.append(";");
            Comont.updateList.add(stringBuilder.toString());
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 需要写入的文件的路径
        String filePath = "src/main/resources/" + requestParm.getTableName().toLowerCase() + ".sql";
        try {
            File file = new File(filePath);
            FileOutputStream fos = null;
            if (!file.exists()) {
                file.createNewFile();// 如果文件不存在，就创建该文件
            }
            fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");// 指定以UTF-8格式写入文件
            // 遍历list
            for (String date : Comont.updateList) {
                // 写
                osw.write(date);
                // 每写入一个Map就换一行
                osw.write("\r\n");
            }
            // 写入完成关闭流
            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("所有数据解析完成！");

    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.head = headMap;
    }
}

